package com.cheyunkeji.er.activity.fast_evaluate;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.adapter.citymodel.CarBrandSortAdapter;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.fast_evaluate.CarBrandList;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.model.CarBrandModel;
import com.cheyunkeji.er.utils.PinyinComparator;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.cityview.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class CarBrandSelectActivity extends BaseActivity implements SideBar.OnTouchingLetterChangedListener, AdapterView.OnItemClickListener {

    private static final String TAG = CarBrandSelectActivity.class.getSimpleName() ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.lv_car_brand)
    ListView lvCarBrand;
    @BindView(R.id.dialog)
    TextView dialog;
    @BindView(R.id.activity_car_brand_select)
    LinearLayout activityCarBrandSelect;
    @BindView(R.id.sidebar)
    SideBar sidebar;

    @BindView(R.id.ll_car_model_tip)
    LinearLayout ll_car_model_tip;
    @BindView(R.id.tv_car_model_vv)
    TextView tv_car_model_vv;

    private String model_mark;

    @BindView(R.id.tv_locate_brand)
    TextView tvLocateBrand;

    private List<CarBrandList.CarBrand> carList;
    private CarBrandList.CarBrand carBrand;
    private CarBrandSortAdapter adapter;
    private List<CarBrandModel> dataList;

    private String from = "";

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_car_brand_select);
        ButterKnife.bind(this);
        if(getIntent().hasExtra("from")) {
            from = getIntent().getStringExtra("from");
        }
        getCarBrandList();

    }


    private void getCarBrandList() {

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());

        ApiClient.postForm(HttpConstants.CAR_300_BRAND_LIST, params, new RespCallback<List<CarBrandList.CarBrand>>() {

            @Override
            protected void onRequestFailed(String msg) {
                SToast.show(msg);
            }

            @Override
            protected void onSuccess(List<CarBrandList.CarBrand> data) {

                fillCarBrandList(data);

            }

            @Override
            public void onBefore(Request request, int id) {
                showDialog();
            }

            @Override
            public void onAfter(int id) {
                dismissDialog();
            }

        });

    }


    @Override
    protected void initView() {

        vTopbar.setTitle("选择品牌");
        vTopbar.setLeftBack();

        sidebar.setTextView(dialog);
        sidebar.setTextColor(getResources().getColor(R.color.half_transparent_blue_2));

        if(getIntent().hasExtra("Mark")){
            model_mark = getIntent().getStringExtra("Mark");
        }
        if (!TextUtils.isEmpty(model_mark)){
            ll_car_model_tip.setVisibility(View.VISIBLE);
            tv_car_model_vv.setText(model_mark);
        }
    }

    @Override
    protected void initData() {


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tv_locate_brand:
                if (null != carBrand) {
                    Intent intent = new Intent(CarBrandSelectActivity.this, FastEvaluateCarSeriesSelectActivity.class);
                    intent.putExtra(Constants.SELECTED_CAR_BRAND_NAME, carBrand);
                    startActivity(intent);

                    MyApplication.getInstance().getCar300Ids().setCarBrandId(this.carBrand.getId());
                }
                break;

            default:
                break;
        }
    }

    private void fillCarBrandList(List<CarBrandList.CarBrand> carList) {

        if (carList != null) {
            this.carList = carList;
            dataList = new ArrayList<>();
            ArrayList<String> indexString = new ArrayList<>();

            for (int i = 0; i < carList.size(); i++) {

                if (carList.get(i).getIs_default()==1){
                    carBrand = new CarBrandList.CarBrand();
                    this.carBrand = carList.get(i);
//                    tvLocateBrand.setText(carBrand.getName());
                }

                CarBrandModel model = new CarBrandModel();
                model.setName(carList.get(i).getName());
                model.setInitial(carList.get(i).getInitial());
                model.setId(carList.get(i).getId());

                if (model.getInitial().matches("[A-Z]")) {
                    if (!indexString.contains(model.getInitial())) {
                        indexString.add(model.getInitial());
                    }
                }

                dataList.add(model);
            }

            Collections.sort(indexString);
            sidebar.setIndexText(indexString);
            Collections.sort(dataList, new PinyinComparator());
            adapter = new CarBrandSortAdapter(this, dataList);
            lvCarBrand.setAdapter(adapter);
            sidebar.setVisibility(View.VISIBLE);
            initEvents();

        }
    }

    private void initEvents() {

        sidebar.setOnTouchingLetterChangedListener(this);
        lvCarBrand.setOnItemClickListener(this);
//        tvLocateBrand.setOnClickListener(this);
    }


    @Override
    public void onTouchingLetterChanged(String s) {

        //该字母首次出现的位置
        int position = adapter.getPositionForSection(s.charAt(0));
        if (position != -1) {
            lvCarBrand.setSelection(position);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        CarBrandModel model = dataList.get(position) ;

        Intent intent = new Intent(CarBrandSelectActivity.this, FastEvaluateCarSeriesSelectActivity.class);
//        intent.putExtra(Constants.SELECTED_CAR_BRAND_NAME, this.carList.get(position));
        intent.putExtra("from",from);
        intent.putExtra("Mark", model_mark);
        intent.putExtra(Constants.SELECTED_CAR_BRAND_NAME, new CarBrandList.CarBrand(model.getId(),model.getName(),model.getInitial(),0));

        startActivity(intent);

        MyApplication.getInstance().getCar300Ids().setCarBrandId(this.carList.get(position).getId());
    }



}
